const path = require("path");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { DefinePlugin } = require("webpack");

module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "./dist"),
    clean: true,
  },
  resolve: {
    modules: ["node_modules"],
    extensions: [".js", ".jsx", ".vue"],
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  target: "web",
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader", "postcss-loader"],
      },
      {
        test: /\.less$/,
        use: ["style-loader", "css-loader", "less-loader", "postcss-loader"],
      },
      {
        test: /\.m?js$/,
        loader: "babel-loader",
      },
      {
        test: /\.(png|jpe?g|gif|svg)$/i,
        type: "asset",
        parser: {
          dataUrlCondition: {
            maxSize: 1 * 20,
          },
        },
        generator: {
          filename: "img/[name]_[hash:5].[ext]",
        },
      },
    ],
  },
  plugins: [
    new DefinePlugin({
      // 默认会注入process, 用于区分开发环境还是生成环境
      BASE_URL: "'./'",
      why: '"why"',
    }),
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: "./public/index.html",
      title: "电商项目",
    }),
  ],
};
